<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/7 11:37
 * Desc: 会员折扣
 */
class Model_Api_V2_Marketing_Strategy_Member_Discount extends Contract_Api_Marketing_Strategy
{
    private static $instance = null;

    private $application_name = '会员价';

    private $application_code = 'member_discount';

    private $icon = '&#xe63f;';

    private $mid;

    private $type_id;

    public function __construct($type_id, $params = [])
    {
        $this->type_id = $type_id;
        $this->mid     = $params['mid'] ?? 0;
    }

    /**
     * @param int $type_id
     * @param array $params
     * @return Model_Api_V2_Marketing_Strategy_Member_Discount|null
     */
    public static function get_instance(int $type_id, array $params = [])
    {
        // 应用是否安装
        if (!St_Functions::is_normal_app_install('memberdiscount')) {
            return null;
        }

        if (is_null(self::$instance)) {
            self::$instance = new self($type_id, $params);
        }

        return self::$instance;
    }

    public function is_support($product_id)
    {
        $price = Model_Api_V2_Discount::get_discount($this->mid, $this->type_id, $product_id, 100);

        if ($price > 0) {
            return array(
                'code' => $this->application_code,
                'name' => $this->application_name,
                'icon' => $this->icon,
            );
        }

        return false;
    }
}